package FDUFW.localhost.Sys;

//过滤指定后缀名的文件，如过滤为java的文件。
import java.io.File;
import java.io.FileFilter;

public class ListRootsTest {
	//ArrayList fileNames=new ArrayList();
	void listRoots() {
		File[] roots = File.listRoots();
		for (int i = 0; i < roots.length; i++) {
			System.out.println(roots[i]);
		}
	}

	void listDirectory(String dir, final String file_ex) {
		File f = new File(dir);
		File[] files;
		if (file_ex.equals("*")) {
			files = f.listFiles();
		} else {
			files = f.listFiles(new FileFilter() {
				public boolean accept(File pathname) {
					String strName = pathname.getName();
					int i = strName.lastIndexOf(".");
					if (pathname.isDirectory())
						return true;
					if (i > 0 && strName.substring(i + 1).toLowerCase().equals(file_ex))
						return true;
					else
						return false;
				}
			});

		}
		if (files == null)
			return;
		for (int i = 0; i < files.length; i++) {
			if (files[i].isDirectory()) {
				//System.out.println("-----------"+files[i]+" is a directory has more files bellow:");
				listDirectory(files[i].getAbsolutePath(), file_ex);
			}

			else {
				System.out.println(files[i]);
			}
		}
	}

	public static void main(String[] args) {
		ListRootsTest lrt = new ListRootsTest();
		System.out.println("-----------" + "start list roots" + "----------");
		lrt.listRoots();
		System.out.println("-------" + "start list directory d:" + "-------");
		//lrt.listDirectory(File.separator);
		//lrt.listDirectory("D:\\myjava\\netTest","java");
		//lrt.listDirectory("D:\\myjava","java");
		//lrt.listDirectory("D:"+File.separator,"java");
		lrt.listDirectory("x:\\", "java");
		//lrt.listDirectory("D:\\myjava\\netTest","*");

	}

}

//在listFiles里面加了一个过滤器， lrt.listDirectory("D:\\myjava\\netTest","java");过滤该目录下所有java文件， lrt.listDirectory("D:\\myjava\\netTest","*");列出所有文件。

